//
//  Register_ViewController.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "Register_ViewController.h"

@interface Register_ViewController ()
//手机号
@property (nonatomic, strong) UITextField * phoneTel_TF;
//验证码
@property (nonatomic ,strong) UITextField * verify_TF;
//倒计时
@property (nonatomic, strong) UIButton * countDownTime_Btn;
//点击同意
@property (nonatomic, strong) UIButton * agreeBox_Btn;
//同意协议
@property (nonatomic, strong) UILabel * agree_Label;
//登录下一步
@property (nonatomic, strong) UIButton * Rigster_Btn;

@property (nonatomic, strong) NSTimer * timer;

@property (nonatomic, assign) NSInteger secondes;

@end

@implementation Register_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp_UI];
}

- (void)viewDidDisappear:(BOOL)animated {

    [super viewDidDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}
#pragma mark - 配置界面
- (void)setUp_UI {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    [self setTitle:@"注册"];
    [self.view addSubview:self.phoneTel_TF];
    [self.view addSubview:self.verify_TF];
    [self.view addSubview:self.countDownTime_Btn];
    [self.view addSubview:self.agree_Label];
    [self.view addSubview:self.agreeBox_Btn];
    [self.view addSubview:self.Rigster_Btn];
    
    //电话号码 phoneTel_TF
    [self.phoneTel_TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64 + 40);
        make.height.mas_equalTo(35);
        make.left.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    //倒计时
    [self.countDownTime_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneTel_TF.mas_bottom).offset(30);
        make.right.mas_equalTo(self.phoneTel_TF.mas_right);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(100);
    }];
    
    //验证码
    [self.verify_TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.countDownTime_Btn.mas_top);
        make.height.mas_equalTo(35);
        make.left.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.countDownTime_Btn.mas_left).offset(-20);
    }];
    
    //协议
    [self.agreeBox_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.verify_TF.mas_bottom).offset(45);
        make.left.mas_equalTo(self.verify_TF.mas_left).offset(50);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    //协议内容
    [self.agree_Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.agreeBox_Btn);
        make.left.mas_equalTo(self.agreeBox_Btn.mas_right).offset(10);
        make.height.mas_equalTo(self.agreeBox_Btn);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
    
    //登录按钮
    [self.Rigster_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.agreeBox_Btn.mas_bottom).offset(20);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(self.view).offset(20);
        make.right.mas_equalTo(self.view).offset(-20);
    }];
}

#pragma mark - Action事件
//点击同意用户协议改变图标
- (void)clickAgreeBoxButton:(UIButton *)button {
    button.selected = !button.isSelected;
}

//点击获去验证码
- (void)clickCountDownTime_Btn: (UIButton *)button {
    self.secondes = 10;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownTime) userInfo:nil repeats:YES];
    [self.timer fire];
}

- (void)clickRigster_Btn:(UIButton *)button {
    NSLog(@"开始注册!");
    
    if (![self.phoneTel_TF.text isEqualToString:@""] && ![self.verify_TF.text isEqualToString:@""]) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"注册成功!" message:@"健康生活每一天!" preferredStyle:UIAlertControllerStyleAlert ];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"开始" style:UIAlertActionStyleDefault handler:nil];
        
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:nil];
        
    } else if ([self.phoneTel_TF.text isEqualToString:@""] || [self.verify_TF.text isEqualToString:@""]) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"请输入手机号码和验证码!" message:@"健康生活每一天!" preferredStyle:UIAlertControllerStyleAlert ];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"知道了!" style:UIAlertActionStyleDestructive handler:nil];
        
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:nil];
    }
}

- (void)countDownTime {
    NSString * stateString = nil;
    if (self.secondes <= 0) {
        self.countDownTime_Btn.userInteractionEnabled = YES;
        stateString = @"获取验证码";
        [self.timer invalidate];
        self.timer = nil;
    } else {
        self.countDownTime_Btn.userInteractionEnabled = NO;
        stateString = [NSString stringWithFormat:@"%lds",self.secondes];
    }
    
    self.countDownTime_Btn.titleLabel.text = @"60s";
    [self.countDownTime_Btn setTitle:stateString forState:UIControlStateNormal];
    self.secondes -= 1;
}

#pragma mark - 懒加载
- (UITextField *)phoneTel_TF {
    if (!_phoneTel_TF) {
        _phoneTel_TF = [[UITextField alloc] init];
        _phoneTel_TF.placeholder = @"请输入你的手机号码";
        _phoneTel_TF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneTel_TF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _phoneTel_TF;
}

- (UITextField *)verify_TF {
    if (!_verify_TF) {
        _verify_TF = [[UITextField alloc] init];
        _verify_TF.placeholder = @"请输入验证码";
        _verify_TF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _verify_TF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _verify_TF;
}

- (UIButton *)agreeBox_Btn {
    if (!_agreeBox_Btn) {
        _agreeBox_Btn = [[UIButton alloc] init];
        [_agreeBox_Btn setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
        [_agreeBox_Btn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
        [_agreeBox_Btn addTarget:self action:@selector(clickAgreeBoxButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBox_Btn;
}

- (UILabel *)agree_Label {
    if (!_agree_Label) {
        _agree_Label = [[UILabel alloc] init];
        _agree_Label.font = FONT(14);
        
        NSString * agreeText = @"同意并已经阅读用户协议";
        NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:agreeText];
        NSRange range = [agreeText rangeOfString:@"用户协议"];
        [attribute addAttributes:@{NSFontAttributeName:FONT(14),NSForegroundColorAttributeName:BLUE_COLOR} range:range];
        _agree_Label.attributedText = attribute;
    }
    return _agree_Label;
}

- (UIButton *)countDownTime_Btn {
    if (!_countDownTime_Btn) {
        _countDownTime_Btn = [[UIButton alloc] init];
        [_countDownTime_Btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _countDownTime_Btn.backgroundColor = [UIColor lightGrayColor];
        _countDownTime_Btn.layer.cornerRadius = 3;
        _countDownTime_Btn.layer.masksToBounds = YES;
        _countDownTime_Btn.titleLabel.font = FONT(16);
        [_countDownTime_Btn addTarget:self action:@selector(clickCountDownTime_Btn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _countDownTime_Btn;
}

- (UIButton *)Rigster_Btn {
    if (!_Rigster_Btn) {
        _Rigster_Btn  = [[UIButton alloc] init];
        [_Rigster_Btn setTitle:@"注册" forState:UIControlStateNormal];
        [_Rigster_Btn setBackgroundColor:kThemeColor];
        [_Rigster_Btn.titleLabel setFont:FONT(20)];
        [_Rigster_Btn addTarget:self action:@selector(clickRigster_Btn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Rigster_Btn;
}

@end




















